import {
  getOtherUserHomeInfo,
  setFollow,
  getTaAnswerList,
  getExpertEvaluate,
} from "@/static/js/my.js";

export default {
  data() {
    return {
      taUserInfo: {}, //主要信息
      answerList: [],
      //认证专业人士
      majorTypes: "",
      majorIsOnline: 0, //是否在线
      majorDirectionList: [],
      listStatus: "noMore",
      isRefreshing: false, //界面下拉的状态
      currentPage: 0,
      navList: [
        {
          name: "TA的回答",
        },
        {
          name: "评价",
        },
      ],
      evaluateList: [],
      id: "",
      page: 1,
      pagesize: 10,
      is_focus: 0,
      allCount: 0,
      showModal: false, //老师不在线或者忙碌确认弹窗
      content: "当前老师不在线，是否继续提问", //弹窗文本
    };
  },
  onLoad(e) {
    if (e.id) {
      this.id = e.id;
    } else {
      this.is = this.taUserInfo.id;
    }
    this.getMyUserHomeInfo();
    this.getExpertEvaluate();
    this.getTaAnswerList();
  },
  methods: {
    //前往回答会话页面
    goInfo(item) {
      uni.navigateTo({
        url: "/quiz/askInfo/index?id=" + item.id,
      });
    },
    goAsk() {
      if (this.majorIsOnline == 0 || this.majorIsOnline == 2) {
        this.showModal = true;
        this.content =
          this.majorIsOnline == 0
            ? "当前老师不在线，是否继续提问"
            : this.majorIsOnline == 2
            ? "当前老师忙碌中，是否继续提问"
            : "";
      } else {
        this.onConfirm();
      }
    },
    onConfirm() {
      this.showModal = false;
      uni.navigateTo({
        url: "/quiz/ask/index?id=" + this.id,
      });
    },
    navClick(index) {
      this.currentPage = index;
    },

		loadMore() {
      if (this.listStatus == "more") {
				this.page++;
				this.getTaAnswerList();
        this.getMyUserHomeInfo();
        this.getExpertEvaluate();
      }
    },

    //获取主页信息
    async getMyUserHomeInfo() {
      var data = {
        uid: this.id,
        page: this.page,
        pagesize: this.pagesize,
      };
      let res = await getOtherUserHomeInfo(data);
      if (res.code == 1) {
        this.taUserInfo = res.data.info;
        this.is_focus = res.data.is_focus;
        this.majorTypes = res.data.major_types;
        this.majorIsOnline = res.data.major_is_online;
        if (res.data.info && res.data.info.major_direction) {
          this.majorDirectionList = res.data.info.major_direction
            .split(",")
            .slice(0, 4);
        } else {
          this.majorDirectionList = [];
        }
        this.isRefreshing = false;
      } else {
        this.$tools.msg(res.msg);
      }
    },

    //获取回答列表
    async getTaAnswerList() {
      var data = {
        user_id: this.id,
        page: this.page,
        pagesize: this.pagesize,
      };
      const res = await getTaAnswerList(data);
      console.log("回答列表", res);
      let arr = [];
      if (this.page == 1) {
        this.answerList = [];
      }
      let newList = res.data;
      if (newList.length >= this.pagesize) {
        this.listStatus = "more";
      } else {
        this.listStatus = "noMore";
      }

      newList.forEach((val) => {
        let obj = {
          answer: JSON.parse(val.first_answer[0].message),
          name: val.quser_nickname,
          time: val.add_time_text,
          avatar: val.quser_avatar,
          content: val.title,
          id: val.id,
        };
        this.answerList.push(obj);
      });
    },

    //获取评价
    async getExpertEvaluate() {
      var data = {
        user_id: this.id,
        page: this.page,
        pagesize: this.pagesize,
      };
      const res = await getExpertEvaluate(data);
      if (res.code == 1) {
        this.allCount = res.data.evaluatedatas_count;

        if (this.page == 1) {
          this.evaluateList = [];
        }
        let newList = res.data.evaluatedatas;
        if (newList.length >= this.pagesize) {
          this.listStatus = "more";
        } else {
          this.listStatus = "noMore";
        }
        this.evaluateList.push(...newList);

        this.evaluateList.forEach((val) => {
          if (val.star == 5 && val.evaluate == "") {
            val.evaluate = "系统默认好评!";
          }
        });
      }
    },

    //关注
    async gzFun() {
      let params = {
        uid: this.id,
      };
      let res = await setFollow(params);
      if (res.code == 1) {
        this.$tools.msg(this.is_focus == 1 ? "取消成功" : "关注成功");
        this.is_focus = 1 - this.is_focus;
      } else {
        this.$tools.msg(res.msg);
      }
    },
    goBack() {
      uni.navigateBack({
        delta: 1,
      });
    },

    goFans(type) {
      uni.navigateTo({
        url: "/pages/my/homeCenter/fans?id=" + this.id + "&type=" + type,
      });
    },
  },
};